<template>
  <div style="margin-bottom: 20px;">
    <el-dialog title="库区调整" append-to-body v-dialogDrag :visible.sync="edit_flag"
               @open="handleOpenConfig" :before-close="handleClose" width="500px">
      <el-form class="ml30" ref="sendMsgRef" :rules="rules" :model="createObj" label-width="100px">
        <el-form-item v-if="edit_main_title === '单个'" label="仓库">
          <el-input v-model="createObj.storeHouseName" class="w200" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item v-if="edit_main_title === '单个'" label="原库区编码">
          <el-input v-model="createObj.oldAreaName" class="w200"  :disabled="true"></el-input>
        </el-form-item>
        <el-form-item v-if="edit_main_title === '单个'" label="原库位编码">
          <el-input v-model="createObj.oldLocationNo" class="w200"  :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="库区" prop="newAreaId">
          <el-select v-model="createObj.newAreaId" class="dialog_input"
                     filterable reserve-keyword placeholder="请选择">
            <el-option v-for="item in newLocationList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-button type="primary" @click="handleClose">取消</el-button>
        <el-button type="primary" :disabled="createConfirmBtnFlag" @click="createConfirmClick">确定</el-button>
      </el-row>
      <div style="height:20px"></div>
    </el-dialog>
  </div>
</template>

<script type="text/javascript">
  import * as FactoryPreparationAPI from '@/api/factoryPreparation/factoryPreparation'
  export default {
    name: '',
    components: {
    },
    props: ['edit_flag', 'edit_main_title', 'edit_select_row', 'edit_params'],
    data() {
      return {
        title_data: {},
        table_data: [],
        createConfirmBtnFlag: false, // 按钮
        newLocationList: [], //  库位
        createObj: {
          storeHouseName: null, // 仓库 名称
          oldAreaName: null, // 旧 库区 名称
          oldLocationNo: null, // 旧 库位 名称
          storeHouseId: null, // 仓库 id
          newAreaId: null // 库区 id
        },
        rules: {
          newAreaId: [
            { required: true, message: '请选择库区', trigger: 'change' }
          ]
        }
      }
    },
    methods: {
      // 弹窗页打开初始
      handleOpenConfig() {
        console.log('this.edit_select_row', this.edit_select_row)
        const { storeHouseName, areaName, locationNo, storeHouseId } = this.edit_select_row[0]
        this.createObj = {
          storeHouseName: storeHouseName,
          oldAreaName: areaName,
          oldLocationNo: locationNo,
          newAreaId: null,  // 目的库区的id
          storeHouseId: storeHouseId // 仓库 id
        }
        this.newLocationList = []
        // this.createDialogVisible = true
        this.createConfirmBtnFlag = false
      },
      // 仓库-库区查询
      getHouseList(storeHouseId) {
        FactoryPreparationAPI.queryMovementArea({ storeHouseId: storeHouseId }).then(result => {
          if (result.code === 0) {
            if (result.data && result.data.length > 0) {
              this.newLocationList = result.data
            } else {
              this.$message({
                message: '查询结果无库区',
                type: 'error',
                duration: 2000
              })
            }
          } else {
            this.$message({
              title: '错误提示',
              message: result.message,
              type: 'error',
              duration: 2000
            })
          }
        })
      },
      // 库位调整 提交
      createConfirmClick() {
        const that = this
        that.$refs['sendMsgRef'].validate(valid => {
          if (valid && that.checkObj(that.createObj)) {
            that.createConfirmBtnFlag = true
            const now_arr = []
            that.edit_select_row.map(item => now_arr.push(item.id)) // factoryId  ： 无单主键id
            const toParams = {
              houseId: that.createObj.storeHouseId,
              newAreaId: that.createObj.newAreaId,
              factoryId: now_arr.length > 1 ? now_arr.join(',') : now_arr[0]
            }
            FactoryPreparationAPI.updateFactoryLocation(toParams).then(result => {
              if (result.code === 0) {
                this.$message({
                  message: '库位调整提交成功',
                  type: 'success',
                  duration: 2000
                })
                this.createConfirmBtnFlag = false
                // this.createDialogVisible = false
                // this.getList()
                this.$emit('edit-confirm', false)
              } else {
                this.$message({
                  title: '错误提示',
                  message: result.message,
                  type: 'error',
                  duration: 2000
                })
                this.createConfirmBtnFlag = false
              }
            }).catch(() => {
              this.createConfirmBtnFlag = false
            })
          }
        })
      },
      checkObj(obj) {
        const { newAreaId } = obj
        // console.log('newAreaId', newAreaId)
        if (!newAreaId) {
          this.$message({
            message: '请选择库区',
            type: 'warning',
            duration: 2000
          })
          return false
        }
        return true
      },
      handleClose() {
        this.$emit('edit-close', false)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
